package com.superm.service.impl;

import com.superm.entity.*;
import com.superm.mappers.EmpDutiesMapper;
import com.superm.mappers.RoleMapper;
import com.superm.mappers.RolePerMapper;
import com.superm.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Service
@Transactional(propagation = Propagation.REQUIRED)
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RolePerMapper rolePerMapper;
    @Autowired
    private EmpDutiesMapper empDutiesMapper;
    @Override
    public List<Role> AllRole() {
        return roleMapper.AllRole();
    }

    @Override
    public List<RolePermission> allPerId(int id) {
        return roleMapper.allPerId(id);
    }

    @Override
    public Role queryId(int id) {
        return roleMapper.queryId(id);
    }

    @Override
    public Integer upRole(Role role) {
        return roleMapper.upRole(role);
    }

    @Override
    public Integer addRole(Role role) {
        return roleMapper.addRole(role);
    }
    @Override
    public Integer delRole(String rids) {
        List<String> idList = Arrays.asList(rids.split(","));
        if(empDutiesMapper.queryIds(idList)>0){
            return 0;
        }else{
            rolePerMapper.delPers(idList);
            roleMapper.delRole(idList);
        };
        return 100;
    }


    /**rui
     * map格式查询全部用户
     * @return
     */
    @Override
    public List<RolePoJo> selectAllRoleMap() {
        List<RolePoJo> listRole = new ArrayList<RolePoJo>();
        List<Role> roleList = roleMapper.AllRole();
        roleList.forEach(role->{
            RolePoJo rolePoJo = new RolePoJo(role.getRid().toString(),role.getrName());
            listRole.add(rolePoJo);
        });
        return listRole;
    }


    /**
     * 实现根据用户Id查询角色信息
     * @param emp
     * @return
     */
    public List<String> selectAllRole(Emp emp){
        List<String> userHaveRole = new ArrayList<String>();
        List<Role> roleList = roleMapper.allRole(emp.geteId());
        roleList.forEach(role->{
            userHaveRole.add(role.getRid().toString());
        });
        return userHaveRole;
    }
}
